package com.jeecg.demo.service.impl;

import com.jeecg.demo.entity.XmDemandstructEntity;
import com.jeecg.demo.entity.XmPlaceinfoEntity;
import com.jeecg.demo.service.XmPlaceinfoServiceI;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("xmPlaceinfoService")
@Transactional
public class XmPlaceinfoServiceImpl extends CommonServiceImpl implements XmPlaceinfoServiceI {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
 	public void delete(XmPlaceinfoEntity entity) throws Exception{
 		super.delete(entity);
 	}
	public void addMain(XmPlaceinfoEntity xmPlaceinfo,
	        List<XmDemandstructEntity> xmDemandstructList) throws Exception{
			//保存主信息
			this.save(xmPlaceinfo);
		
			/**保存-煤炭需求结构信息*/
			for(XmDemandstructEntity xmDemandstruct:xmDemandstructList){
				//外键设置
				xmDemandstruct.setPlaceId(xmPlaceinfo.getId());
				this.save(xmDemandstruct);
			}
	}

	public void updateMain(XmPlaceinfoEntity xmPlaceinfo,
	        List<XmDemandstructEntity> xmDemandstructList) throws Exception {
		//保存主表信息
		if(StringUtil.isNotEmpty(xmPlaceinfo.getId())){
			try {
				XmPlaceinfoEntity temp = findUniqueByProperty(XmPlaceinfoEntity.class, "id", xmPlaceinfo.getId());
				MyBeanUtils.copyBeanNotNull2Bean(xmPlaceinfo, temp);
				this.saveOrUpdate(temp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			this.saveOrUpdate(xmPlaceinfo);
		}
		//===================================================================================
		//获取参数
		Object id0 = xmPlaceinfo.getId();
		//===================================================================================
		//1.查询出数据库的明细数据-煤炭需求结构信息
	    String hql0 = "from XmDemandstructEntity where 1 = 1 AND placeId = ? ";
	    List<XmDemandstructEntity> xmDemandstructOldList = this.findHql(hql0,id0);
		//2.筛选更新明细数据-煤炭需求结构信息
		if(xmDemandstructList!=null&&xmDemandstructList.size()>0){
		for(XmDemandstructEntity oldE:xmDemandstructOldList){
			boolean isUpdate = false;
				for(XmDemandstructEntity sendE:xmDemandstructList){
					//需要更新的明细数据-煤炭需求结构信息
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-煤炭需求结构信息
		    		super.delete(oldE);
	    		}
	    		
			}
			//3.持久化新增的数据-煤炭需求结构信息
			for(XmDemandstructEntity xmDemandstruct:xmDemandstructList){
				if(oConvertUtils.isEmpty(xmDemandstruct.getId())){
					//外键设置
					xmDemandstruct.setPlaceId(xmPlaceinfo.getId());
					this.save(xmDemandstruct);
				}
			}
		}
	}

	public void delMain(XmPlaceinfoEntity xmPlaceinfo) throws Exception{
		//删除主表信息
		this.delete(xmPlaceinfo);
		//===================================================================================
		//获取参数
		Object id0 = xmPlaceinfo.getId();
		//===================================================================================
		//删除-煤炭需求结构信息
	    String hql0 = "from XmDemandstructEntity where 1 = 1 AND placeId = ? ";
	    List<XmDemandstructEntity> xmDemandstructOldList = this.findHql(hql0,id0);
		this.deleteAllEntitie(xmDemandstructOldList);
		
	}
 	
}